<template>
  <el-dialog
    title="反向授权"
    :visible.sync="dialogVisible"
    width="60%"
    top="15vh"
    :before-close="handleClose"
  >
    <div>
      <el-row>
        <el-col :span="24">
          <div style="float: right; margin-bottom: 10px">
            <el-button
              type="primary"
              @click="personAdd"
              icon="el-icon-document-add"
              plain
              >&nbsp;新增</el-button
            >
          </div>
        </el-col>
      </el-row>
      <vxe-table
        style="margin-bottom: 10px"
        :align="allAlign"
        :data="tableData"
        highlight-current-row
        @current-change="selectChange"
        border
      >
        <vxe-column type="seq" width="60"></vxe-column>
        <vxe-column field="personName" title="姓名"></vxe-column>
        <vxe-column field="idCard" title="身份证号"></vxe-column>
        <vxe-column
          field="jobShort"
          title="工作单位及职务"
        ></vxe-column>
        <vxe-column title="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              @click="personDelete(scope.row)"
              icon="el-icon-delete"
              plain
              >&nbsp;删除</el-button
            >
          </template>
        </vxe-column>
      </vxe-table>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
    </span>

    <el-dialog
      width="60%"
      :title="innerTitle"
      :visible.sync="visible"
      append-to-body
      top="15vh"
      :before-close="handleCloseInner"
    >
      <el-row>
        <el-col :span="7">
          <radioOrgTree @init="init" ref="dialogRadioOrgTree" />
        </el-col>
        <el-col :span="17">
          <vxe-table
            border
            header-align="center"
            ref="mytable"
            :height="heightY"
            :loading="loading"
            highlight-current-row
            highlight-hover-row
            :export-config="{ modes: ['current', 'selected', 'all'] }"
            :data="tableDataInner"
            @checkbox-all="selectAllEvent"
            @checkbox-change="selectChangeEvent"
          >
            <vxe-table-column
              field="xuanzhong"
              type="checkbox"
              width="60"
              align="center"
            ></vxe-table-column>
            <!--        <vxe-table-column field="xuhao" type="seq" title="序号" width="60" align="center" fixed="left"></vxe-table-column>-->
            <vxe-table-column
              type="html"
              field="xingming"
              title="姓名"
              width="100"
              align="center"
              sortable
            ></vxe-table-column>
            <vxe-table-column
              field="sfzh"
              title="身份证号"
              width="200"
              align="center"
              sortable
            ></vxe-table-column>
          <vxe-table-column
            field="zhiwu_jiancheng"
            title="工作单位及职务"
            width="500"
            sortable
          >
            <template #default="{ row }">
              <span>{{ row.zhiwu_jiancheng }}</span>
            </template>
          </vxe-table-column>
          <vxe-table-column
            field="csny"
            title="出生年月"
            cell-type="string"
            align="center"
            width="120"
            sortable
          ></vxe-table-column>
          <vxe-table-column
            field="cjgzsj"
            title="参加工作时间"
            cell-type="string"
            align="center"
            width="160"
            sortable
          ></vxe-table-column>
          <vxe-table-column
            field="qrzxl_zg"
            title="全日制教育"
            align="center"
            width="180"
            sortable
          ></vxe-table-column>
          <vxe-table-column
            field="qrzxlxx_zg"
            title="毕业院校系及专业"
            align="center"
            width="300"
            sortable
          ></vxe-table-column>
          <vxe-table-column
            field="zzxl_zg"
            title="在职教育"
            align="center"
            width="180"
            sortable
          ></vxe-table-column>
          <vxe-table-column
            field="zzxlxx_zg"
            title="毕业院校系及专业"
            align="center"
            width="300"
            sortable
          ></vxe-table-column>
            <template #empty>
              <span style="color: #000">
                <p>暂无数据</p>
              </span>
            </template>
          </vxe-table>
        </el-col>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleCloseInner">取 消</el-button>
        <el-button type="primary" @click="personAddSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </el-dialog>
</template>

<script>
import {
  getRolePersonDelete,
  getRolePersonList,
  getRolePersonAdd,
} from "@/api/datacentersg/achives/documentApi";
import radioOrgTree from "@/views/datacentersg/common/radioOrgTreelazy";
import store from "@/store";
import { getisAllow } from "@/api/datacentersg/achives/documentApi";
import { getPersonByOrg } from "@/api/datacentersg/person/person";
export default {
  components: {
    radioOrgTree,
  },
  data() {
    return {
      tableData: [],
      tableDataInner: [],
      allAlign: "center",
      dialogVisible: false,
      heightY: window.innerHeight - 170 + "px",
      roleId: "",
      visible: false,
      dataForm: {
        personName: "",
        roleId: "",
        idCard: "",
        jobShort: "",
      },
      innerTitle: "",
      role: "",
      loading: false,
      selectAllEventList: [],
      multipleSelection: [],
      jg_id: "",
      is_contains_org_child: false,
      selectedNode: {},
    };
  },
  created() {
    
  },
  methods: {
    async init(node) {
      this.loading = true;
      this.jg_id = node.id;
      let isContainsOrgChild = "1";
      if (this.is_contains_org_child == false) {
        isContainsOrgChild = "0";
      }
      if (node === "init") {
        this.loading = false;
      } else if (node === "select") {
        let data = this.$refs.dialogRadioOrgTree.node_had.childNodes[0].data;
        this.selectedNode = {
          label: data.jg_mingcheng,
          id: data.jg_id,
          code: data.jg_bianma,
          children: [],
          parentId: data.jg_shangji,
          type: data.jg_leixing,
          icon: data.icon,
          shortName: data.jgjc,
        };
      } else {
        let params = {
          userId: store.getters.user_id.toString(),
          token: store.getters.token.toString(),
          data: {
            orgId: node.id,
          },
        };
        getisAllow(params).then((result) => {
          if (result.resultdata == true) {
            const userInfo =
              JSON.parse(sessionStorage.getItem(`userInfo`)) || {};
            let personJsons = {
              userId: store.getters.user_id.toString(),
              token: store.getters.token.toString(),
              isEnableEditOrg: userInfo.isEnableEditOrg.toString(),
              system: "datacenter",
              is_contains_org_child: isContainsOrgChild,
              data: {
                page: 1,
                rows: 20,
                jg_id: node.id,
                xingming: null,
                sfzh: null,
                gllb_dm: "",
                glzt_dm: "1",
              },
            };
            getPersonByOrg(personJsons)
              .then((response) => {
                if (response.result) {
                  this.tableDataInner = response.resultdata.data; // 填充列表数据
                } else {
                  this.tableDataInner = [];
                  this.$message.error(response.message);
                }
                this.loading = false;
              })
              .catch(() => {
                this.$message.error("内部错误！");
              });
          } else if (result.resultdata == false) {
            this.$message({
              message: "无权限查看",
              type: "warning",
            });
            this.loading = false;
          }
        });
      }
    },
    initSelect(params) {
      const that = this;
      that.roleId = params.roleId;
      that.getPersonList();
      this.dialogVisible = true;
    },
    selectAllEvent(personRow) {
      this.selectAllEventList = [];
      this.multipleSelection = [];
      this.selectAllEventList = personRow.records;
    },
    selectChangeEvent({ checked, records }) {
      this.selectAllEventList = [];
      this.multipleSelection = [];
      this.multipleSelection = records;
    },
    handleCloseInner(){
      this.visible = false;
      this.selectAllEventList = [];
      this.multipleSelection = [];
      this.tableDataInner = [];
      this.$refs.dialogRadioOrgTree.requestNewData();
    },
    handleClose() {
      this.dialogVisible = false;
    },
    selectChange(currentRow) {
      this.role = currentRow.row;
    },
    personAdd() {
      this.visible = true;
      this.innerTitle = "新增";
      this.init("init");
    },
    personAddSubmit() {
      const dataList = [...this.multipleSelection,...this.selectAllEventList];
      const params = {
        userId: store.getters.user_id.toString(),
        token: store.getters.token.toString(),
        data:[],
      };
      dataList.forEach((item) => {
        params.data.push({
          roleId: this.roleId,
          idCard: item.sfzh,
          personName:item.xingming,
          jobShort: item.zhiwu_jiancheng,
        })
      });
      getRolePersonAdd(params).then((result) => {
        if (result.result) {
          this.$message({
            message: "操作成功",
            type: "success",
          });
          this.visible = false;
          this.getPersonList();
          this.tableDataInner = [];
          this.$refs.dialogRadioOrgTree.requestNewData();
        }
      });
    },
    getPersonList() {
      const params = {
        userId: store.getters.user_id.toString(),
        token: store.getters.token.toString(),
        data: {
          roleId: this.roleId, //必传
        },
      };

      getRolePersonList(params).then((result) => {
        if (result.result) {
          this.tableData = result.resultdata.data;
        }
      });
    },
    personDelete(row) {
      this.$confirm("确定删除当前行?", "操作提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        const params = {
          userId: store.getters.user_id.toString(),
          token: store.getters.token.toString(),
          data: {
            id: row.id,
          },
        };

        getRolePersonDelete(params).then((result) => {
          if (result.result) {
            this.$message({
              type: "success",
              message: "操作成功",
            });
            this.getPersonList();
          }
        });
      });
    },
  },
};
</script>

<style scoped>
>>>.vxe-body--row.row--hover{
  z-index: 10000 !important;
}
>>>.vxe-table--tooltip-wrapper {
    z-index: 10000 !important;
  }
  >>>.vxe-table--tooltip-wrapper .theme--dark .placement--top .is--visible .is--arrow{
    z-index: 10000 !important;
  }
  >>>.vxe-tooltip {
  z-index: 9999;
}
>>>.el-tooltip__popper{ z-index:9999;}
>>>.vxe-table--tooltip-wrapper.theme--dark.placement--top.is--visible.is--arrow{


}
</style>